﻿using System.Security.Policy;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Simple.Common.EventBus;
using Simple.Common.Services;
using Simple.Services.Account.Models;
using Simple.Services.EventHandlers;
using Simple.Services.System.MorningCheck;
using Simple.Services.System.MorningCheck.Models;

namespace Simple.WebApi.Controllers.System
{
    /// <summary>
    ///  
    /// </summary>
    [Route("api/equipment/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class ClockingInMorningCheckController : ControllerBase
    {
        private readonly ClockingInMorningCheckService _clockingInMorningCheckService;
        private readonly IEventPublisher _eventPublisher;
        private readonly ISimpleService _simpleService;
        public ICurrentUserService _currentUser;

        private readonly IHttpContextAccessor _httpContextAccessor;

        public ClockingInMorningCheckController(ClockingInMorningCheckService clockingInMorningCheckService, IEventPublisher eventPublisher, ISimpleService simpleService,
            IHttpContextAccessor httpContextAccessor)
        {
            _clockingInMorningCheckService = clockingInMorningCheckService;
            _eventPublisher = eventPublisher;
            _simpleService = simpleService;
            _currentUser = _simpleService.GetService<ICurrentUserService>();
            this._httpContextAccessor = httpContextAccessor;

        }

        /// <summary>
        /// 单个数据到考勤机
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> SingleSyncData([FromBody] SingleSyncDataQueryModel model)
        {
            var data = await _clockingInMorningCheckService.SingleSyncDataAsync(model);
            return AppResult.Status200OK(data: data);
        }


        /// <summary>
        /// 批量同步数据到考勤机
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> SyncData([FromBody] BatchSyncDataQueryModel model)
        {
            model.OrganizationId = _currentUser.OrganizationId;
            var @event1 = new DeviceEventModel();
            @event1.syncDataQueryModel = model;
            _eventPublisher.PublishAsync(@event1).GetAwaiter().GetResult();

            return AppResult.Status200OK("成功");
        }

        /// <summary>
        /// 同步学生数据到晨检机
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> SyncChildrensMorningCheck()
        {
            var @event1 = new DeviceEventModel();
            @event1.batchSyncChildrensMorningCheckQueryModel = new BatchSyncChildrensMorningCheckQueryModel();
            @event1.batchSyncChildrensMorningCheckQueryModel.OrganizationId = _currentUser.OrganizationId;
            _eventPublisher.PublishAsync(@event1).GetAwaiter().GetResult();
            return AppResult.Status200OK("成功");
        }



        /// <summary>
        /// 测试考勤回调
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<AppResult> AttendanceCallback([FromBody] string dataStr)
        {
            await _clockingInMorningCheckService.AttendanceCallback(dataStr);
            return AppResult.Status200OK("成功");
        }

        /// <summary>
        /// 删除考勤机所有人员名单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> DeleteAllUser([FromBody] BatchSyncDataQueryModel model)
        {
            await _clockingInMorningCheckService.DeleteAllUser(model);
            return AppResult.Status200OK("成功");
        }

        /// <summary>
        /// 获取同步结果
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet] 
        public async Task<AppResult> GetSyncCallBack(string messageId)
        {
            var data = await _clockingInMorningCheckService.GetSyncCallBack(messageId);
            return AppResult.Status200OK(data: data);
        }

    }
}
